package base.items;

import java.io.File;
import java.util.HashSet;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

/**
 * The Music {@code class}. Create Music object extended from an Item.
 * 
 * @author Peter NGO, Guillaume POUSSEL and Mathieu RUL
 * 
 * @version 1.0
 */
public class Music extends Item {

	/**
	 * Artist of the Music.
	 */
	private String artist;

	/**
	 * Length of the Music.
	 */
	private double length;

	/**
	 * Constructor of the {@code class}. Create a Music from an ID.
	 * 
	 * @param ID
	 *            the Music ID.
	 */
	public Music(Integer ID) {
		super(ID);
		setType("Music");
	}

	/**
	 * Constructor of the {@code class}. Create a Music from parameter.
	 * 
	 * @param ID
	 *            the Music ID.
	 * 
	 * @param title
	 *            the Music title.
	 * 
	 * @param tags
	 *            the Music tags.
	 * 
	 * @param comments
	 *            the Music comments.
	 * 
	 * @param artist
	 *            the Music artist.
	 * 
	 * @param length
	 *            the Music length.
	 */
	public Music(Integer ID, String title, HashSet<Integer> tags,
			HashSet<Integer> comments, String artist, double length) {
		super(ID, "Music", title, tags, comments);
		this.artist = artist;
		this.length = length;
	}

	/**
	 * Returns a {@code String} array with the allowed MIME types for a Music.
	 * 
	 * @return a string array with the allowed MIME.
	 */
	@Override
	public String[] allowedMimeTypes() {
		return new String[] { "audio/mpeg", "audio/x-ms-wma",
				"audio/vnd.rn-realaudio", "audio/x-wav" };
	}

	/**
	 * Get the artist of the Music.
	 * 
	 * @return the artist.
	 */
	public String getArtist() {
		return artist;
	}

	/**
	 * Get the length of the Music.
	 * 
	 * @return the length.
	 */
	public double getLength() {
		return length;
	}

	/**
	 * Parse music data for extracting the music's length.
	 * 
	 * @return {@code true} if the length was correctly extracted; {@code false}
	 *         otherwise.
	 */
	public boolean parseData() {
		if (!super.parseData())
			return false;
		File fichier = new File(getPath());
		AudioInputStream ais;
		try {
			ais = AudioSystem.getAudioInputStream(fichier);
			AudioFormat audioformat = ais.getFormat();
			setLength(ais.getFrameLength() * audioformat.getFrameSize()
					/ (60 * audioformat.getFrameRate()));
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * Set the artist of the Music.
	 * 
	 * @param artist
	 *            the artist to set.
	 */
	public void setArtist(String artist) {
		this.artist = artist;
	}

	/**
	 * Set the length of the Music.
	 * 
	 * @param length
	 *            the length to set.
	 */
	public void setLength(double length) {
		this.length = length;
	}

	/**
	 * Returns a {@code String} object representing the Music's value.
	 * 
	 * @return a string representation of the Music.
	 */
	@Override
	public String toString() {
		return super.toString() + "\nArtist: " + artist + "\nLength: " + length
				+ " min";
	}

}
